package cn.edu.zafu.bigdata.lwg;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentInfoSystem {
    private static List<Student> studentList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    retrieveStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序。");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入学生年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        System.out.print("请输入学生学号: ");
        String studentId = scanner.nextLine();

        Student student = new Student(name, age, studentId);
        studentList.add(student);
        System.out.println("学生信息添加成功。");
    }

    private static void retrieveStudent() {
        System.out.print("请输入要检索的学生学号: ");
        String studentId = scanner.nextLine();
        for (Student student : studentList) {
            if (student.getStudentId().equals(studentId)) {
                System.out.println("学生姓名: " + student.getName());
                System.out.println("学生年龄: " + student.getAge());
                System.out.println("学生学号: " + student.getStudentId());
                return;
            }
        }
        System.out.println("未找到该学生信息。");
    }

    private static void updateStudent() {
        System.out.print("请输入要更新的学生学号: ");
        String studentId = scanner.nextLine();
        for (Student student : studentList) {
            if (student.getStudentId().equals(studentId)) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的年龄: ");
                int age = scanner.nextInt();
                scanner.nextLine(); // 消费换行符
                student.setName(name);
                student.setAge(age);
                System.out.println("学生信息更新成功。");
                return;
            }
        }
        System.out.println("未找到该学生信息。");
    }

    private static void deleteStudent() {
        System.out.print("请输入要删除的学生学号: ");
        String studentId = scanner.nextLine();
        boolean removed = studentList.removeIf(student -> student.getStudentId().equals(studentId));
        if (removed) {
            System.out.println("学生信息删除成功。");
        } else {
            System.out.println("未找到该学生信息。");
        }
    }
}

class Student {
    private String name;
    private int age;
    private String studentId;

    public Student(String name, int age, String studentId) {
        this.name = name;
        this.age = age;
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
}